
//@"{\"userName\":\"19957101664@163.com\",\"smsCode\":\"744739\",\"clientId\":\"428a8310cd442757ae699df5d894f051\",\"phonenumber\":\"19957101664@163.com\",\"grantType\":\"sms\",\"userType\":\"app_user\"}"

enum LoginGrantType: String, CaseIterable, Codable {
    case password = "password";
    case sms = "sms";           // 手机号注册， 手机号验证码登陆，邮箱验证码登陆
    case email = "email";       // 邮箱注册
    case social = "social";   // 三方登录，微信 apple等
    case phone = "phone";  // 一键登录，
}

enum RequestCodeType: String, Codable { // 发送验证码时的类型
    case register = "1"      // 注册
    case notRegister = "2"        // 登陆，修改密码，绑定
}

enum ValidateCodeType: String, Codable { // 校验验证码时的类型
    case register = "1"      // 注册
    case modifyPwd = "2"        //修改密码
    case loginOrBinding = "3"        // 登陆，绑定三方帐号
}

enum AuthAPI {
    // 账号密码登录
    struct LoginPwd: API {
        struct Body: Encodable {
            let userName: String
            let password: String
            let userType = "app_user"
            let grantType = LoginGrantType.password.rawValue
            let clientId = APP_CLIENTID
        }
        struct Response: Decodable {
            let access_token: String
            let client_id: String
        }
        let body: Body?
        let path: String = "/auth-client/login"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["Content-Type": "text/plain", "isencrypt": "true", "clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 微信登录
    struct WechatLogin: API {
        struct Body: Encodable {
            let socialCode: String
            let socialState: String
            let userType = "app_user"
            let grantType = LoginGrantType.social.rawValue
            let clientId = APP_CLIENTID
            let source = "wechat_open"
            let userName: String?
        }
        struct Response: Decodable {
            let access_token: String
            let client_id: String
        }
        let body: Body?
        let path: String = "/auth-client/login"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["Content-Type": "text/plain", "isencrypt": "true", "clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 苹果apple登录
    struct AppleLogin: API {
        struct Body: Encodable {
            let appleUser: String
            let socialState: String
            let socialCode: String
            let userType = "app_user"
            let grantType = LoginGrantType.social.rawValue
            let clientId = APP_CLIENTID
            let source = "appleid"
        }
        struct Response: Decodable {
            let access_token: String
            let client_id: String
        }
        let body: Body?
        let path: String = "/auth-client/login"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["Content-Type": "text/plain", "isencrypt": "true", "clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 一键登录
    struct OneKeyLogin: API {
        struct Body: Encodable {
            let token: String // 从阿里通信sdk获取的toke
            let userType = "app_user"
            let grantType = LoginGrantType.phone.rawValue
            let clientId = APP_CLIENTID
        }
        struct Response: Decodable {
            let access_token: String
            let client_id: String
        }
        let body: Body?
        let path: String = "/auth-client/login"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["Content-Type": "text/plain", "isencrypt": "true", "clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 验证码登录
    struct SMSLogin: API {
        struct Body: Encodable {
            let phonenumber: String
            let smsCode: String
            let userType = "app_user"
            let grantType = LoginGrantType.sms.rawValue
            let clientId = APP_CLIENTID
        }
        struct Response: Decodable {
            let access_token: String
            let client_id: String
        }
        let body: Body?
        let path: String = "/auth-client/login"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["Content-Type": "text/plain", "isencrypt": "true", "clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 发送短信验证码
    struct SMSSend: API {
        struct Body: Encodable {
            let phoneNumber: String
        }
        struct Response: Decodable {
        }
        let body: Body?
        let path: String = "/auth-client/integration-auth/sendSmsVerifyCode"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = false
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 发送邮箱验证码
    struct EmailCodeSend: API {
        struct Body: Encodable {
            let email: String
            let type: RequestCodeType  // 1 注册，2 登陆，绑定，修改密码等等，除注册以外
        }
        struct Response: Decodable {
        }
        let body: Body?
        let path: String = "/auth-client/sendEmailVerificationCode"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = false
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 校验验证码
    struct ValidateCode: API {
        struct Body: Encodable {
            let userName: String
            let code: String
            let type: ValidateCodeType // 1 注册，2 登陆，绑定，修改密码等等，除注册以外

        }
        typealias Response = Bool 
        let body: Body?
        let path: String = "/auth-client/validateCode"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = false
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 更新密码
    struct UpdatePwd: API {
        struct Body: Encodable {
            let userName: String // 手机号或者邮箱
            let newPassword: String
            let clientId = APP_CLIENTID
            let userType = "app_user"

        }
        struct Response: Decodable {
        }
        let body: Body?
        let path: String = "/auth-client/updatePwd"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["clientId": APP_CLIENTID, "Authorization": "Bearer " + (AuthManager.shared.getToken() ?? "")]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
   
    // 注册
    struct Register: API {
        struct Body: Encodable {
            let userName: String
            let password: String
            let grantType: LoginGrantType
            let clientId = APP_CLIENTID
            let userType = "app_user"
        }
        
        struct Response: Decodable {}
        
        let body: Body?
        let path: String = "/auth-client/register"
        let method: HTTPMethod = .POST
        var headers: [String: String] = ["clientId": APP_CLIENTID]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = true
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
    
    // 获取用户信息,用来登录tuya,涂鸦账号密码登录
    struct UserLogInInfo: API {
        var body: EmptyBody?
        
        struct Response: Decodable {
            let userId: String
            let userType: String
            let userName: String
            let nickName: String
            let password: String
        }
        let path: String = "/auth-client/user/info"
        let method: HTTPMethod = .GET
        var headers: [String: String] = ["clientId": APP_CLIENTID, "Authorization": "Bearer " + (AuthManager.shared.getToken() ?? "")]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = false
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }

    
    // 获取用户三方授权信息
    struct SocialList: API {
        typealias Response = [Data]
        struct Data: Decodable {
            let id: String
            let userId: String
            let authId: String
            let source: String
            let userName: String    // 微信账号显示
            let nickName: String  //
            let email: String    // 三方邮箱，苹果账号显示
            let avatar: String  // 三方头像
        }
        let body: EmptyBody?
        let path: String = "/auth-client/social/list"
        let method: HTTPMethod = .GET
        var headers: [String: String] = ["clientId": APP_CLIENTID, "Authorization": "Bearer " + (AuthManager.shared.getToken() ?? "")]
        let query: [String: String]? = nil
        let pathValues: [String: String]? = nil
        let requiresEncryption: Bool = false
        
        func withUpdatedHeaders(_ headers: [String: String]) -> Self {
            var copy = self
            copy.headers = headers
            return copy
        }
    }
}

